<?php


namespace Tools\Utils;


class Html
{

    function referer()
    {
        return $_SERVER['HTTP_REFERER'] ?: U();
    }

    //文章内容获取HTML内容
    public static function getHtmlString($content)
    {
        $content = preg_replace("#<script[^>]*?>.*?</script\s*?>#is", "", $content);
        return htmlspecialchars_decode(html_entity_decode(stripcslashes($content), ENT_COMPAT, 'UTF-8'));
    }

    /**
     * 将内容以文件形式输出
     *
     * @param string $filename
     * @param string $data
     * @return bool
     */
    public static function forceDownload($filename = '', $data = '')
    {
        if ($filename == '' or $data == '') {
            return false;
        }

        if (false === strpos($filename, '.')) {
            return false;
        }

        $mime = 'application/octet-stream';

        // 用户使用是否IE
        if (strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== false) {
            $filename = rawurlencode($filename);
            header('Content-Type: "' . $mime . '"');
            header('Content-Disposition: attachment; filename="' . $filename . '"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header("Content-Transfer-Encoding: binary");
            header('Pragma: public');
            header("Content-Length: " . strlen($data));
        } else {
            header('Content-Type: "' . $mime . '"');
            header('Content-Disposition: attachment; filename="' . $filename . '"');
            header("Content-Transfer-Encoding: binary");
            header('Expires: 0');
            header('Pragma: no-cache');
            header("Content-Length: " . strlen($data));
        }

        exit($data);
    }
}